home *** CD-ROM | disk | FTP | other *** search
- /*
- ** CTelnetInterp.h
- **
- ** TurboTCP support library
- ** Generic Telnet protocol interpreter
- **
- ** Copyright © 1993-94, FrostByte Design / Eric Scouten
- **
- */
-
-
- #pragma once
-
- #include "TCL.h"
- #include "CTCPEndpoint.h"
- #include "Telnet.protocol.h"
-
-
- // state variable for the Telnet command parser
-
- typedef unsigned char uchar;
- typedef enum TelnetState {
- normalChar, // interpret as character
- gotIAC, // received an IAC character
- gotSB, // in subnegotiation
- gotWILL, // received a WILL option
- gotWONT, // received a WONT option
- gotDO, // received a DO option
- gotDONT, // received a DONT option
- gotIACinSB // received IAC while in SB
- } TelnetState;
-
- #define sbBfrMax 80 // max size of subnegotiation buffer
- #define lineBfrMax 80 // max size of line buffer
-
-
- /*______________________________________________________________________
- **
- ** CTelnetInterpreter
- **
- ** This abstract class is a specialized TCP protocol interpreter. It is a subclass of the
- ** CTCPEndpoint mix-in class which implements the basic Telnet protocol. It may be
- ** used to implement command-line protocols which are based on the Telnet protocol
- ** (i.e., NNTP or FTP).
- **
- ** This class provides no user interface behaviors and assumes no character-based
- ** terminal. You will need to subclass this class to interpret the specific protocol
- ** you are implementing. For an example, see the CTelnetTerminal class in the MiniTelnet
- ** application.
- **
- ** NOTE: This class is provided only as a convenience. You need not include it in your project.
- **
- ** TurboTCP 1.0 NOTE: This class replaces the CTelnetInterpreter class in version 1.0.
- **
- **
- */
-
- class CTelnetInterp : public CTCPEndpoint {
-
- TCL_DECLARE_CLASS;
-
- protected:
- TelnetState itsState; // current command parser state
- short sbBfrIndex; // current index to subnegotiation buffer
- char sbBfr[sbBfrMax]; // subnegotiation buffer
- short lineBfrIndex; // current index to linemode buffer
- char lineBfr[lineBfrMax]; // linemode buffer
- Boolean useLineBfr; // hold all characters until full line received
- Boolean showDebug; // show debugging codes
-
-
- // constructor
-
- public:
- CTelnetInterp(unsigned short theDefaultPort,
- unsigned long recBufferSize = recReceiveSize,
- unsigned short autoReceiveSize = recAutoRecSize,
- unsigned short autoReceiveNum = recAutoRecNum,
- Boolean doUseCName = TRUE);
-
-
- // respond to incoming data
-
- // Override these methods to place characters on the terminal window or
- // interpret them. All Telnet commands are processed before these methods
- // are called (i.e. the character stream sent to these methods is intended
- // directly for the terminal). Both methods *must* be implemented.
- //
- // HandleNVTChar receives a single character.
- // HandleNVTLine receives a pointer to a C string (maximum 81 characters)
- // which may or may not be terminated by /n or /r.
-
- virtual void HandleNVTChar(uchar theChar) = 0;
- virtual void HandleNVTLine(char* theLine) = 0;
-
-
- // Telnet command handling
-
- // If you wish to respond to specific Telnet commands, override these methods.
- // You may ignore any or all of these methods.
-
- virtual void ReceivedWill(uchar theOption);
- // default method responds [WONT <option>]
- virtual void ReceivedWont(uchar theOption) {}
- virtual void ReceivedDo(uchar theOption);
- // default method responds [WONT <option>]
- virtual void ReceivedDont(uchar theOption) {}
- virtual void ReceivedBRK() {}
- virtual void ReceivedSynch() {}
- virtual void ReceivedIP() {}
- virtual void ReceivedAO() {}
- virtual void ReceivedAYT() {}
- virtual void ReceivedEC() {}
- virtual void ReceivedEL() {}
- virtual void ReceivedGA() {}
- virtual void ReceivedSB(uchar theChar);
- // default method collects characters until [IAC SE] is received
- // (characters are ignored unless ReceivedSE is overriden)
- virtual void ReceivedSE() {}
-
-
- // debugging methods
-
- // Override these methods if you wish to use the debugging features built into
- // this class. If the showDebug field is set, you these methods will receive
- // notifications such as [IAC EC] when Telnet commands are received over
- // the data stream.
- //
- // These methods were useful to me in debugging the implementation of various Telnet
- // options. In a terminal-based method (such as MiniTelnet’s CTelnetTerminal), these
- // should be hooked up to routines that print the relevant data to the display screen.
- //
- // PrintDebugStr() should display a C string to the terminal.
- // PrintDebugCharNum() should display a bracketed character number to
- // the terminal (i.e. if you call PrintDebugCharNum('!', '[', ']'), [33]
- // should be written to the terminal).
-
- virtual void PrintDebugStr(char* theDebugStr) {}
- virtual void PrintDebugCharNum(char theChar, char leftBracket, char rightBracket) {}
-
-
- // respond to incoming data — do not override these methods
-
- private:
- virtual void HandleDataArrived(void* theData, unsigned short theDataSize, Boolean isUrgent);
- virtual void ReceivedIAC(uchar theCommand);
-
- };
-